//
//  BLEssenceTableViewController.m
//  百思不得姐
//
//  Created by hbl on 16/3/14.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLEssenceTableViewController.h"
#import "ViewController.h"
#import "BLTopicTableViewController.h"
@interface BLEssenceTableViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak)UIView *topView;
@property (nonatomic, strong)UIButton *currentBtn;
@property (nonatomic, weak)UIView *redBar;
@property (nonatomic, weak)UIScrollView *contentView;

@end

@implementation BLEssenceTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNav];
    
    [self setupTopView];
    
    [self setupChildViewController];
    
    [self setupContentView];
    
    self.view.backgroundColor = BLBg;
}

- (NSString *)nearTopic{
    return  @"list";
}

- (void)setupContentView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *contentView = [[UIScrollView alloc]init];
    contentView.frame = self.view.bounds;
//    contentView.backgroundColor = [UIColor redColor];
    [self.view insertSubview:contentView atIndex:0];
    contentView.contentSize = CGSizeMake(self.view.frame.size.width * self.childViewControllers.count, 0);
    contentView.pagingEnabled = YES;
    contentView.delegate = self;
    self.contentView = contentView;
    [self scrollViewDidEndScrollingAnimation:contentView];
}

- (void)setupChildViewController
{
    BLTopicTableViewController *allVc = [[BLTopicTableViewController alloc]init];
    allVc.type = @"1";
    allVc.nearTopic = self.nearTopic;
    [self addChildViewController:allVc];
    
    BLTopicTableViewController *videoVc = [[BLTopicTableViewController alloc]init];
    videoVc.type = @"41";
     videoVc.nearTopic = self.nearTopic;
    [self addChildViewController:videoVc];
    
    BLTopicTableViewController *photoVc = [[BLTopicTableViewController alloc]init];
    photoVc.type = @"10";
     photoVc.nearTopic = self.nearTopic;
    [self addChildViewController:photoVc];
    
    BLTopicTableViewController *wordsVc = [[BLTopicTableViewController alloc]init];
    wordsVc.type = @"29";
     wordsVc.nearTopic = self.nearTopic;
    [self addChildViewController:wordsVc];
    
    BLTopicTableViewController *soundsVc = [[BLTopicTableViewController alloc]init];
    soundsVc.type = @"31";
     soundsVc.nearTopic = self.nearTopic;
    [self addChildViewController:soundsVc];
    
}

- (void)setupTopView
{
    UIView *topView = [[UIView alloc]init];
    topView.frame = CGRectMake(0, 64, self.view.frame.size.width, 35);
    [self.view addSubview:topView];
    topView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.85];
    self.topView = topView;
    
    //添加红色指示器
    UIView *redBar = [[UIView alloc]init];
    [redBar setBackgroundColor:[UIColor redColor]];
    CGFloat redBarH = 2.0;
    
    
    
    NSArray *buttonNameArray = @[@"全部",@"视频",@"图片",@"段子",@"声音"];
    CGFloat titleBtnW = topView.frame.size.width /buttonNameArray.count;
    CGFloat titleBtnH = topView.frame.size.height;
    CGFloat titleBtnY = 0;
    for (int i = 0; i < buttonNameArray.count; i++) {
        UIButton *titleBtn = [[UIButton alloc]init];
        titleBtn.tag = i;
        CGFloat titleBtnX = titleBtnW * i;
        titleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [titleBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [titleBtn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        titleBtn.frame = CGRectMake(titleBtnX, titleBtnY, titleBtnW, titleBtnH);
        [titleBtn setTitle:buttonNameArray[i] forState:UIControlStateNormal];
        
        [titleBtn addTarget:self action:@selector(topBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [topView addSubview:titleBtn];
        
        if (i == 0) {
            [titleBtn.titleLabel sizeToFit];
            CGFloat redBarW = titleBtn.titleLabel.frame.size.width;
            redBar.bounds = CGRectMake(0, 0, redBarW, redBarH);
            redBar.center = CGPointMake(titleBtn.center.x, titleBtn.frame.size.height - redBarH);
            self.redBar = redBar;
            [self topBtnClick:titleBtn];
        }
        
    }
    
    [self.topView addSubview:redBar];
    
    
}

- (void)topBtnClick:(UIButton *)btn
{    
    //btn点击变色
    self.currentBtn.enabled = YES;
    btn.enabled = NO;
    self.currentBtn = btn;
   [UIView animateWithDuration:0.25 animations:^{
        self.redBar.center = CGPointMake(btn.center.x, btn.frame.size.height - self.redBar.frame.size.height);
   }];
  
    [self.contentView setContentOffset:CGPointMake(btn.tag * self.view.frame.size.width, 0) animated:YES ];
    
}


- (void)setupNav
{
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MainTitle"]];
    UIButton *leftBtn = [self setItemButtonWithImage:@"MainTagSubIcon" selImage:@"MainTagSubIconClick"];
    [leftBtn addTarget:self action:@selector(leftBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
}

- (UIButton *)setItemButtonWithImage:(NSString *)image selImage:(NSString *)selImage
{
    UIButton *btn = [[UIButton alloc]init];
    [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:selImage] forState:UIControlStateHighlighted];
    btn.bounds = CGRectMake(0, 0, btn.currentBackgroundImage.size.width, btn.currentBackgroundImage.size.height);
    
    return btn;
}

- (void)leftBtnOnClick
{
    [self.navigationController pushViewController:[[ViewController alloc]init] animated:YES];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x/self.contentView.frame.size.width;
    
    UITableViewController *vc = self.childViewControllers[index];
    CGFloat viewX = scrollView.contentOffset.x;
    CGFloat viewY = 0;
    CGFloat viewH = self.contentView.frame.size.height;
    CGFloat viewW = self.contentView.frame.size.width;
    vc.view.frame = CGRectMake(viewX, viewY, viewW, viewH);
   
    [self.contentView addSubview:vc.view];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    int index = scrollView.contentOffset.x/self.view.frame.size.width;
    [self topBtnClick:self.topView.subviews[index]];
}

@end
